package com.jdone.compus.controller;

import com.jdone.compus.dto.ApiResponse;
import com.jdone.compus.model.StudentWhitelist;
import com.jdone.compus.service.StudentWhitelistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Optional;

@RestController
@RequestMapping("/student-whitelist")
@Api(tags = "学生白名单管理")
public class StudentWhitelistController {

    @Autowired
    private StudentWhitelistService service;

    @ApiOperation("创建学生白名单")
    @PostMapping
    public ApiResponse createStudent(@RequestBody StudentWhitelist student) {
        try {
            StudentWhitelist createdStudent = service.createStudent(student);
            return new ApiResponse(true, "创建成功", createdStudent);
        } catch (Exception e) {
            return new ApiResponse(false, "创建失败: 学生号可能已存在！");
        }
    }

    @ApiOperation("通过ID获取学生白名单信息")
    @GetMapping("/{id}")
    public ApiResponse getStudentById(@PathVariable Long id) {
        Optional<StudentWhitelist> student = service.getStudentById(id);
        if (student.isPresent()) {
            return new ApiResponse(true, "查询成功", student.get());
        } else {
            return new ApiResponse(false, "未找到该学生");
        }
    }

    @ApiOperation("更新学生白名单信息")
    @PutMapping("/{id}")
    public ApiResponse updateStudent(@PathVariable Long id, @RequestBody StudentWhitelist student) {
        try {
            StudentWhitelist updatedStudent = service.updateStudent(id, student);
            return new ApiResponse(true, "更新成功", updatedStudent);
        } catch (Exception e) {
            return new ApiResponse(false, "更新失败: " + e.getMessage());
        }
    }

    @ApiOperation("删除学生白名单信息")
    @DeleteMapping("/{id}")
    public ApiResponse deleteStudent(@PathVariable Long id) {
        try {
            service.deleteStudent(id);
            return new ApiResponse(true, "删除成功", null);
        } catch (Exception e) {
            return new ApiResponse(false, "删除失败: " + e.getMessage());
        }
    }


    @ApiOperation("通过姓名和房间号分页查询学生白名单信息")
    @GetMapping("/search")
    public ApiResponse searchStudents(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String room,
            @RequestParam(required = false) String studentId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        Pageable pageable = PageRequest.of(page - 1, size);
        Page<StudentWhitelist> result = service.findByNameAndRoomWithPagination(name, room, studentId, pageable);
        return new ApiResponse(true, "查询成功", result);
    }

    @ApiOperation("通过Excel文件批量导入学生白名单信息")
    @PostMapping("/import")
    public ApiResponse importStudentsFromExcel(@RequestParam("file") MultipartFile file) {
        try {
            service.importStudents(file);
            return new ApiResponse(true, "导入成功！", null);
        } catch (Exception e) {
            return new ApiResponse(false, "导入失败：" + e.getMessage());
        }
    }

    @ApiOperation("验证学生白名单")
    @GetMapping("/validate")
    public ApiResponse validateStudent(@RequestParam("studentId") String studentId) {
        Optional<StudentWhitelist> student = service.findByStudentId(studentId);
        if (student.isPresent()) {
            StudentWhitelist whitelist = student.get();
            if (!whitelist.getIsDeleted()) {
                return new ApiResponse(true, "学生存在", whitelist);
            } else {
                return new ApiResponse(false, " 该宿舍暂未开通热水淋浴充值");
            }
        } else {
            return new ApiResponse(false, " 该宿舍暂未开通热水淋浴充值");
        }
    }

    @ApiOperation("通过学生号删除学生记录")
    @DeleteMapping("/delete/{studentId}")
    public ApiResponse deleteStudentById(@PathVariable String studentId) {
        try {
            service.deleteByStudentId(studentId);
            return new ApiResponse(true, "删除成功", null);
        } catch (RuntimeException e) {
            return new ApiResponse(false, "删除失败：" + e.getMessage());
        }
    }
}


